using Gameplay.PVE.Entity;
using Gameplay.PVE.Utils;
using UnityEngine;

namespace Gameplay.PVE
{
    //跟随的行走位置
    public class FollowRoadPath : UnitRoadPath
    {
        private float reactionTime;
        private float startReactionTime;
        
        private Vector3 velocity;
        private Vector3 lastFollowVelocity;
        private Vector3 lastFollowPosition;
        private Vector3 lastFollowForward;

        private Vector3 lastRepathTarget;
        private bool isFollowing = false;
        
        public override void Init(UnitBase unit)
        {
            base.Init(unit);
            actionName = "Run";
            actionSpeed = 1f;
        }
        public void RefreshFollow()
        {
            isFollowing = false;
        }
        
        
        public override void OnTransmit(ETransmitType type,BaseTransmitArg arg)
        {
            base.OnTransmit(type,arg);
            if (type == ETransmitType.Attack)
            {
                if (isFollowing)
                {
                    RefreshFollow();
                }
            }
            else if (type == ETransmitType.RePath)
            {
                needRepath = true;
            }
        }
        
        public override void Update()
        {
            base.Update();
            if (unit.Data.isInSpecialCase)
            {
                MoveToTargetPosition(unit.Data.targetPosition);
            }
            else if (unit.Data.lockTarget != null)
            {
                unit.Data.targetPosition = unit.Data.lockTarget.Data.position;
                var canRepath = (!isFindPathSuccess || TimeManager.logicTime - lastRepathTime > 0.5f);
                if (canRepath)
                {
                    UpdateAStarPath(unit.Data.position,unit.Data.targetPosition,true);
                    lastRepathTarget = unit.Data.targetPosition;
                    lastRepathTime = TimeManager.logicTime;
                }
                UpdateRoadPath();
                startReactionTime = -1;
            }
            else if (unit.Data.followTarget != null)
            {
                CheckFollow();
            }
            else
            {
                MoveToTargetPosition(unit.Data.targetPosition);
            }
            if (position == unit.Data.position)
            {
                unit.Data.targetMoveSpeed = 0;
                unit.Data.moveState = 0;
                isFinish = true;
            }
        }
        
        private void CheckFollow()
        {
            var target = unit.Data.followTarget;
            if (target != null && (unit.Data.stage == PveManager.Instance.stageId || unit.Data.team == 0))
            {
                if (unit.Data.lockTarget == null && unit.Data.followTarget.Data.lockTarget != null)
                {
                    unit.Data.SetLockTarget(unit.Data.followTarget.Data.lockTarget);
                    unit.Transmit(ETransmitType.SearchSuccess);
                }
                
                if (unit.Data.team == 2)
                {
                    return;
                }
                
                if (lastFollowPosition == Vector3.zero || lastFollowForward == Vector3.zero || Vector3.Angle(lastFollowForward,unit.Data.followTarget.Data.targetForward) > 40)
                {
                    lastFollowPosition = unit.Data.followTarget.Data.position;
                    lastFollowForward = unit.Data.followTarget.Data.targetForward;
                    return;
                }

                if (startReactionTime == -1)
                {
                    startReactionTime = TimeManager.logicTime;
                    reactionTime = UnityEngine.Random.Range(0.5f,1);
                    return;
                }
                var followVelocity = unit.Data.followTarget.Data.position - lastFollowPosition;
                //if (Vector3.Distance(followVelocity,lastFollowVelocity) > 0.01f && startReactionTime == 0)//需要变化跟随位置
                {
                    //startReactionTime = TimeManager.logicTime;
                   
                }
                
                lastFollowPosition = unit.Data.followTarget.Data.position;
                lastFollowVelocity = followVelocity;
                if (startReactionTime != 0 && TimeManager.logicTime - startReactionTime > reactionTime)
                {
                    startReactionTime = 0;
                    velocity = lastFollowVelocity.normalized;
                }
                if (startReactionTime == 0)
                {
                    float angle = PveUtils.GetAngle(Vector3.forward,lastFollowForward);
                    //angle = (float)Math.Round(angle / 10) * 10;
                    unit.Data.targetPosition = unit.Data.followTarget.Data.position +
                                               Quaternion.AngleAxis(angle, Vector3.up) * unit.Data.followOffset;
                    var d = (unit.Data.position - target.Data.position).sqrMagnitude;
                    /*if (d < unit.Data.followOffsetDistanceSqr)
                    {
                        
                    }
                    else */if (d < unit.Data.followOffsetDistanceSqr * 1.2f)
                    {
                        velocity = (lastFollowPosition - unit.Data.position).normalized;
                        MoveToTargetPosition(unit.Data.targetPosition);
                    }
                    else
                    {
                        MoveToTargetPosition(unit.Data.targetPosition,1.5f);
                    }
                }
            }
        }
    }
}